//
//  WeatherFourthView.m
//  weather
//
//  Created by 大L君的无敌战舰 on 16/4/13.
//  Copyright © 2016年 bigL.com. All rights reserved.
//

#import "WeatherFourthView.h"
#import "BLButtonForWeatherFourthView.h"
#import "BLWebViewController.h"



@interface WeatherFourthView ()
/* 保存btn模型 */
@property (strong, nonatomic) NSMutableArray * btnArray;
/* 创建容器View */
@property (strong, nonatomic) UIView *containView;
/* 创建标题 */
@property (strong, nonatomic) UILabel *titleLable;
@end

@implementation WeatherFourthView

-(NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //当前View的配置信息
        [self selfConfigurationInformation];
        //加载子控件
        [self setChildView];
    }
    return self;
}

//当前View的配置信息
- (void)selfConfigurationInformation
{
    //设置背景颜色
    self.backgroundColor = [UIColor clearColor];
}


-(void)setChildView
{
    //创建Label
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.textColor = [UIColor whiteColor];
    titleLable.adjustsFontSizeToFitWidth = YES;
    titleLable.text = @"hahahahhahahaha";
    titleLable.font = TextSmallSize;
    self.titleLable = titleLable;
    [self addSubview:titleLable];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.equalTo(self).offset(BLViewSpace);
    }];
    
    //创建容器View
    UIView *containView = [[UIView alloc]init];
    //    containView.backgroundColor = BLRandomColor(2);
    self.containView = containView;
    [self addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLable.mas_bottom).offset(0);
        make.bottom.equalTo(self).offset(- BLViewSpace);
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
    }];
    
    
    //创建16个天气button
    for (NSInteger i = 0; i<11; i++) {
        UIButton * webBtn = [[BLButtonForWeatherFourthView alloc]init];
        webBtn.userInteractionEnabled = YES;
        [self.containView addSubview:webBtn];
        webBtn.backgroundColor =BLRandomColor(2);
        webBtn.tag = i;
        [webBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArray addObject:webBtn];
    }
}




/**
 *  按钮切换事件 : 传出按钮内的url
 */
-(void)buttonClick:(BLButtonForWeatherFourthView *)button
{
    BLog(@"---------%ld-----",button.tag);
    UIViewController * weatherVC = [self viewController];
    BLWebViewController *webVC = [[BLWebViewController alloc]init];
    [webVC.webView showWebWithURL:button.URL];
    [weatherVC presentViewController:webVC animated:YES completion:nil];
}





-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置btnArray中每个btn的位置
    CGFloat width  = self.width * 0.5;
    CGFloat height = self.containView.height /self.btnArray.count * 2 - 7;
    CGFloat x      = 0;
    CGFloat y      = 0;
    for (NSInteger i = 0; i < self.self.btnArray.count; i++) {
        BLButtonForWeatherFourthView *btn = self.btnArray[i];
        x = width  * (i % 2);
        y = height * (i / 2);
        btn.frame = CGRectMake(x, y, width, height);
    }
    
}



@end
